
// 1. ------------------------------- 获取学生列表 --------------------------------
async function renderList () {
  await axios.get('/students').then(result => {
    // console.log(result.data);
    let data = result.data
    console.log(data)
    let str = ''
    data.forEach(item => {
      str += `
            <tr>
                <td>${item.name}</td>
                <td>${item.age}</td>
                <td>${item.gender ? '女' : '男'}</td>
                <td>第${item.group}组</td>
                <td>${item.hope_salary}</td>
                <td>${item.salary}</td>
                <td>${item.province} ${item.city} ${item.area}</td>
                <td>
                <a href="javascript:;" class="text-success mr-3"><i class="bi bi-pen"></i></a>
                <a href="javascript:;" class="text-danger"><i class="bi bi-trash"></i></a>
                </td>
            </tr>
            `
    })
    document.querySelector('tbody').innerHTML = str
    document.querySelector('.total').innerHTML = result.data.length
  })
}
renderList()

// 2. ------------------------------- 删除操作 --------------------------------
document.querySelector('tbody').addEventListener('click', function (e) {
  // console.log(Array.from(e.target.classList))
  console.log(e.target.classList)
  if (e.target.classList.contains('bi-trash')) {
    let id = e.target.dataset.id
    // console.log(id)
    axios.delete('/students/' + id).then(result => {
      console.log(result.data)
      renderList()
    })
  }
})